
package com.fx.apartment.modules.activity.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fx.apartment.common.support.Condition;
import com.fx.apartment.common.support.Query;
import com.fx.apartment.common.tool.api.R;
import com.fx.apartment.common.tool.utils.Func;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fx.apartment.modules.activity.entity.ActivityParticipation;
import com.fx.apartment.modules.activity.vo.ActivityParticipationVO;
import com.fx.apartment.modules.activity.wrapper.ActivityParticipationWrapper;
import com.fx.apartment.modules.activity.service.IActivityParticipationService;

/**
 * 参与活动 控制器
 *
 * @author QinXueFeng
 * @since 2021-04-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/activityparticipation")
@Api(value = "参与活动", tags = "参与活动接口")
public class ActivityParticipationController {

	private final IActivityParticipationService activityParticipationService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入activityParticipation")
	public R<ActivityParticipationVO> detail(ActivityParticipation activityParticipation) {
		ActivityParticipation detail = activityParticipationService.getOne(Condition.getQueryWrapper(activityParticipation));
		return R.data(ActivityParticipationWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 参与活动
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入activityParticipation")
	public R<IPage<ActivityParticipationVO>> list(ActivityParticipation activityParticipation, Query query) {
		IPage<ActivityParticipation> pages = activityParticipationService.page(Condition.getPage(query), Condition.getQueryWrapper(activityParticipation));
		return R.data(ActivityParticipationWrapper.build().pageVO(pages));
	}


	/**
	 * 新增或修改 参与活动
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入activityParticipation")
	public R submit(@Valid @RequestBody ActivityParticipation activityParticipation) {
		//TODO 用户id
		int num = activityParticipationService.count(Wrappers.<ActivityParticipation>lambdaQuery().eq(ActivityParticipation::getCreateUser,"").eq(ActivityParticipation::getActivityId, activityParticipation.getActivityId()));
		if(num>0){
			R.fail("您已经参加了评选");
		}
		if(Func.isEmpty(activityParticipation.getId())){
			int count = activityParticipationService.count(Wrappers.<ActivityParticipation>lambdaQuery().eq(ActivityParticipation::getActivityId, activityParticipation.getActivityId()));
			if(count<10){
				activityParticipation.setIdentifier("0"+count);
			}else {
				activityParticipation.setIdentifier(String.valueOf(count));
			}
		}
		return R.status(activityParticipationService.saveOrUpdate(activityParticipation));
	}

	
	/**
	 * 删除 参与活动
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(activityParticipationService.deleteLogic(Func.toLongList(ids)));
	}

	
}
